package bank;

//害，算法效率好低呀
public class _07ReversalInt {
    //x是待翻转的整数
    public static int reverse(int x) {
        StringBuilder strx = new StringBuilder(Integer.toString(x));
        if(x >= 0) {
            strx = strx.reverse();
            String str = new String(strx);
            int res = 0;
            try {
                res = Integer.parseInt(str);
            }catch (NumberFormatException e){
                return 0;
            }
            if (-2 * Math.pow(2, 31) <= res && res <= 2 * Math.pow(2, 31) - 1) {
                return (int) res;
            } else {
                return 0;
            }
        }else {
            strx.delete(0,1);
            strx = strx.reverse();
            String str = new String(strx);
            int res = 0;
            try {
                res = Integer.parseInt(str);
            }catch (NumberFormatException e){
                return 0;
            }
            if (-2 * Math.pow(2, 31) <= res && res <= 2 * Math.pow(2, 31) - 1) {
                return (int) ((-1)*res);
            } else {
                return 0;
            }
        }
    }
}
